package com.jsz.peini.controller.app;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gexin.fastjson.JSONObject;
import com.jsz.peini.bean.redDate.RedDate;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.mapper.redDate.RedDateMapper;
import com.jsz.peini.redis.base.RedisConstants;
import com.jsz.peini.redis.base.RedisUtil;
import com.jsz.peini.redis.base.SerializeUtil;
import com.jsz.peini.redis.util.RedDateRedisUtil;


@Controller
public class RedDateController {
	private static final Logger logger = LoggerFactory.getLogger(RedDateController.class);
	@Resource
	private RedDateMapper redDateMapper;
	
	@RequestMapping(value="/redpacketc/list")
	public String list(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="userId",required=false) String userId){
		request.setAttribute("userId", userId);
		 com.alibaba.fastjson.JSONObject obj1 = PeiniUtils.validToken(userId);
			if(obj1.getInteger("resultCode") != 1){
			}
		   userId = obj1.getString("userId");
		return "redpacket/list";
	}
   
   @ResponseBody
   @RequestMapping(value="/redpacketc/takeHb")
   public String takeHb(HttpServletRequest request,HttpServletResponse response,
		   @RequestParam(value="userId",required=false) String userId){
	   JSONObject obj=new JSONObject();
	   try{
		   com.alibaba.fastjson.JSONObject obj1 = PeiniUtils.validToken(userId);
			if(obj1.getInteger("resultCode") != 1){
				obj.put("resultcode", 3);
				obj.put("money", 0);
				return obj.toJSONString();
			}
		   userId = obj1.getString("userId");//是否有问题?
		   StopWatch watch=new StopWatch();
		   watch.start();
		   if(RedisUtil.sadd(RedisConstants.RedDateUserList, userId)==1)
		   {
			  byte[] date= RedisUtil.rpop(RedisConstants.notRedDateList.getBytes());
			  Object dates=SerializeUtil.unserialize(date);
			   if(dates!=null)
			   {
				   RedDate reddate=redDateMapper.selectByPrimaryKey(dates.toString());
				   obj.put("resultcode", 0);
				   obj.put("money", reddate.getVanum());
				   reddate.setUserid(userId);
				   reddate.setCreatetime(new Date());
				   reddate.setType(0);
				   RedDateRedisUtil.addRedDateRedis(reddate);
			   }
			   else
			   {
				   obj.put("money",0);
				   obj.put("resultcode", 2);
			   }
		   }
		   else
		   {
			   obj.put("resultcode", 1);
			   obj.put("money", 0);
		   }
		   watch.stop();
	   } catch (Exception e) {
		   obj.put("resultcode", 4);
		   obj.put("money", 0);
			logger.error(e.getMessage(),e);
		}

	   return obj.toJSONString();
   }
}
